package com.jiinfo.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.base.BaseController;
import com.jiinfo.model.Role;
import com.jiinfo.model.User;
import com.jiinfo.model.UserRole;
import com.jiinfo.vo.BaseVo;

public class UserController extends BaseController{

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@ActionKey("/manage/user")
	public void manage_user(){
		
		Record record=new Record();
		record.set("searchContent", getPara("searchContent"));
		Page<Record> page=User.dao.pagequery(getParaToInt("pageNumber"), getParaToInt("pageSize"), record);
		
		
		setAttr("page", page);
		setAttr("record", record);
		setAttr("pageTitle", getPageTitle("用户管理", "",
				new String[]{"<i class='icon-home'></i>首页","用户管理"},
				new String[]{"/manage","/manage/user/"}));
		renderJsp("/manage/user/user-list.jsp");
	}
	
	
	@ActionKey("/manage/user/add/disp")
	public void manage_user_add_disp(){
		List<Record> roles=Role.dao.getAll();
		
		
		setAttr("roles", roles);
		setAttr("pageTitle", getPageTitle("用户管理", "",
				new String[]{"<i class='icon-home'></i>首页","用户管理","用户新增"},
				new String[]{"/manage","/manage/user/","/manage/user/add/disp"}));
		renderJsp("/manage/user/user-add.jsp");
	}
	
	@ActionKey("manage/user/add")
	public void manage_user_add(){
		
		Record user=getCurrUser();
		String[] cks=getParaValues("ck");
		User vo=getModel(User.class,"vo");
		User.dao.add(vo,cks,user);
		
		redirect("/manage/user/");
		
	}
	
	@ActionKey("/manage/user/edit/disp")
	public void manage_user_edit_disp(){
		String id=getPara("id");
		User user=User.dao.findById(id);
		List<Record> roles=Role.dao.getAll();
		List<Record> userRoles=UserRole.dao.getUserRoleByUserId(id);
		for (Record record : roles) {
			for (Record userRole : userRoles) {
				if(record.getStr("id").equals(userRole.getStr("roleId"))){
					record.set("checked", true);
					break;
				}
			}
		}
		
		setAttr("vo", user);
		setAttr("roles", roles);
		setAttr("pageTitle", getPageTitle("用户管理", "",
				new String[]{"<i class='icon-home'></i>首页","用户管理","用户编辑"},
				new String[]{"/manage","/manage/user/","/manage/user/edit/disp"}));
		renderJsp("/manage/user/user-edit.jsp");
		
	}
	
	@ActionKey("manage/user/edit")
	public void manage_user_edit(){
		Record user=getCurrUser();
		String[] cks=getParaValues("ck");
		User vo=getModel(User.class,"vo");
		User.dao.edit(vo, cks, user);
		redirect("/manage/user/");
		
	}
	
	@ActionKey("/manage/user/remove")
	public void manage_user_remove(){
		
		String id=getPara("id");
		User.dao.remove(id);
		
		redirect("/manage/user/");
		
	}
	
	@ActionKey("/manage/user/isExit")
	public void manage_user_isExit(){
		
		String username=getPara("username");
		Record record=User.dao.getUserByUsername(username);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		if(record!=null){
			baseVo.setData(true);
		}else{
			baseVo.setData(false);
		}
		
		renderJson(baseVo);
	}
}
